local nouns = {} -- makes empty table ya :D
local verbs = {}
local nbuffer = 0
local vbuffer = 0
local buffersize = 1
cowbot = { }  -- cowbot functions
cowbot.chat = { } -- Cowbot functions
function cowbot.messageAll(txt)
	for _, v in pairs(player.GetAll()) do
		v:ChatPrint( txt )
	end
end

function cowbot.init()
	if file.Exists("nouns.txt") then -- does storage exist?
		nouns = string.Explode("|",file.Read("nouns.txt"))
	end
	if file.Exists("verbs.txt") then -- does storage exist?
		verbs = string.Explode("|",file.Read("verbs.txt"))
	end
	cowbot.add( "!cowbot", cowbot.respond )
	cowbot.add( "!dius", cowbot.dius )
	cowbot.add( "!bored", cowbot.bored )
	cowbot.add( "!shank", cowbot.shank )
	cowbot.add( "!nadd", cowbot.nadd )
	cowbot.add( "!vadd", cowbot.vadd )
end

function cowbot.nsave()
	save = string.Implode("|",nouns)
	file.Write("nouns.txt",save)
	nbuffer = 0
end
function cowbot.vsave()
	save = string.Implode("|",verbs)
	file.Write("verbs.txt",save)
	vbuffer = 0
end

function cowbot.respond(ply, line, args)
	if #argv >0 then
	    ULib.tsay(ply,"Just call him cowbot",true)
	    return
	end
	string.Replace( nouns[math.Round(math.random(1,table.Count(nouns)))], "_", " " )
	cowbot.messageAll("Cowbot: Im in your "..string.Replace( nouns[math.Round(math.random(1,table.Count(nouns)))], "_", " " ).." "..string.Replace( verbs[math.Round(math.random(1,table.Count(verbs)))], "_", " " ).." your "..string.Replace( nouns[math.Round(math.random(1,table.Count(nouns)))], "_", " " )..".")
end

function cowbot.bored(ply, line, args)
	if #argv >0 then
	    ULib.tsay(ply,"Just call him cowbot",true)
	    return
	end
	cowbot.messageAll("Cowbot: Make a "..verbs[math.Round(math.random(1,table.Count(verbs)))].." "..nouns[math.Round(math.random(1,table.Count(nouns)))]..".")
end

function cowbot.dius(ply, line, args)
	if #argv >0 then
	    ULib.tsay(ply,"Just call him cowbot",true)
	    return
	end
	local verb = string.Replace( verbs[math.Round(math.random(1,table.Count(verbs)))], "ing", "" )		
	cowbot.messageAll("Get some "..nouns[math.Round(math.random(1,table.Count(nouns)))].." and "..verb.." them to yourself and go as a "..nouns[math.Round(math.random(1,table.Count(nouns)))])
end


function cowbot.shank(ply, line, args)
	if #argv >0 then
	    ULib.tsay(ply,"Just call him cowbot",true)
	    return
	end
	local verb = string.Replace( verbs[math.Round(math.random(1,table.Count(verbs)))], "ing", "" )
	local noun = nouns[math.Round(math.random(1,table.Count(nouns)))]
	local noun2 = nouns[math.Round(math.random(1,table.Count(nouns)))]
	cowbot.messageAll("Try Shanking a"..noun.." with a "..noun2.."!")
end

function cowbot.nadd(ply, line, args)
	if #argv >1 then
	    ULib.tsay(ply,"One Noun at a time",true)
	    return
	end
	insert = argv[ 1 ]
	insert = string.gsub(insert,"_"," ")
	if table.HasValue(nouns,insert) then
		ULib.tsay(ply,"Noun Already Exists sorry",true)
		return
	else
		table.insert(nouns,insert)
		ULib.tsay(ply,"Done!",true)
		nbuffer = nbuffer + 1
		if nbuffer == buffersize then
			nsave()
		end
	end
end

function cowbot.vadd(ply, command, argv, args)
	if #argv >1 then
	    ULib.tsay(ply,"One verb at a time",true)
	    return
	end
	insert = argv[ 1 ]
	insert = string.gsub(insert,"|","")
	if table.HasValue(verbs,insert) then
		ULib.tsay(ply,"verb Already Exists sorry",true)
		return
	else
		table.insert(verbs,insert)
		ULib.tsay(ply,"Done!",true)
		vbuffer = vbuffer + 1
		if vbuffer == buffersize then
			vsave()
		end
	end
end
function cowbot.add(cmd,func) -- this was simpler than some guys code so oh well :D
	cowbot.chat[cmd] = func
end

function cowbot.input( ply, text, team, death )
    if (string.sub(text, 1, 1) == "!") then--if the first 4 letters are /all continue
       local a,err = pcall(cowbot.chat[cmd],prefix,cmd,param,user)
		if not a then
			if err ~= "attempt to call a nil value" then
				irc.msg("heres some lua give it to ahref: "..err)
				return false
			end
		end
    end
end
hook.Add( "PlayerSay", "ahrefcowbotinput", cowbot.input );
cowbot.init()


	